//
//  ImageGalleryScrollView.m
//  AutoShowView
//
//  Created by 冷悦 on 16/2/1.
//  Copyright © 2016年 冷悦. All rights reserved.
//

#import "ImageGalleryScrollView.h"
#import "UIScrollView+Extension.h"
//#import "HomePageViewController.h"

@interface ImageGalleryScrollView () <ImageGalleryDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) NSArray *imageGalleryViews;

@property (nonatomic, strong) NSTimer *timer;

@end

@implementation ImageGalleryScrollView

#pragma mark - Initialize
- (instancetype)init{
    self = [super init];
    [self.timer fire];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startTimerAction) name:MainTimerFireNotification object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopTimerAction) name:MainTimerStopNotification object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(invalidateTimerAction) name:MainTimerInvalidateNotification object:nil];
    return self;
}

#pragma mark - Override
- (void)drawRect:(CGRect)rect{
    self.scrollView.frame = rect;
    [self updateUI];
}

- (void)dealloc{
    [self invalidateTimerAction];
}


#pragma mark - Event Response
- (void)timerAction{
    NSArray *contentViews = [self.scrollView getContentViews];
    NSInteger contentViewsCount = contentViews.count;
    
    if (contentViewsCount > 0) {
        NSInteger viewPos = arc4random() % contentViewsCount;
        ImageGalleryView *imageGalleryView = [contentViews objectAtIndex:viewPos];
        [imageGalleryView updateImageView];
    }
}

#ifndef kImageGalleryScrollViewMargin
#define kImageGalleryScrollViewMargin 10
#endif

#pragma mark - Private
- (void)updateUI{
    if (self.model != nil) {
        CGFloat imageGalleryViewWidth = self.scrollView.frame.size.width*0.37;
        imageGalleryViewWidth = imageGalleryViewWidth == 0?[UIScreen mainScreen].bounds.size.width*0.37:imageGalleryViewWidth;
        CGFloat imageGalleryViewHeight = imageGalleryViewWidth * 3 / 4;
        if (imageGalleryViewWidth > 0) {
            [self.scrollView removeAllSubViews];
            NSInteger modelCount = self.model.count;
            
            for (int i = 0; i < modelCount; i++) {
                id<ImageGalleryModelDelegate> imageGalleryModel = [self.model objectAtIndex:i];
                ImageGalleryView *imageGalleryView = [ImageGalleryView new];
                CGFloat x = (imageGalleryViewWidth + kImageGalleryScrollViewMargin) * i;
                CGRect imageGalleryViewFrame = CGRectMake(x, 0, imageGalleryViewWidth, imageGalleryViewHeight);
                imageGalleryView.frame = imageGalleryViewFrame;
                imageGalleryView.model = imageGalleryModel;
                imageGalleryView.tag = kScrollViewCustomItemViewTag;
                imageGalleryView.delegate = self;
                [self.scrollView addSubview:imageGalleryView];
            }
            
            self.scrollView.contentSize = CGSizeMake((imageGalleryViewWidth + kImageGalleryScrollViewMargin) * modelCount- kImageGalleryScrollViewMargin, imageGalleryViewHeight);
        }
        
    }
}

- (void)startTimerAction{
    [_timer setFireDate:[NSDate date]];
}

- (void)stopTimerAction{
    [_timer setFireDate:[NSDate distantFuture]];
}

- (void)invalidateTimerAction{
    [self.timer invalidate];
    self.timer = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - ImageGalleryViewDelegate
- (void)didTouched:(AutoShowView *)autoShowView{
    id<ImageGalleryModelDelegate> model;
    for (ImageGalleryView *imageGalleryView in self.scrollView.subviews) {
        if (imageGalleryView.autoShowView == autoShowView) {
            model = imageGalleryView.model;
        }
    }
    if (model != nil) {
        if (self.delegate != nil && [self.delegate respondsToSelector:@selector(didSelectImageGalleryViewWithModel:)]) {
            [self.delegate didSelectImageGalleryViewWithModel:model];
        }
    }else{
        NSAssert(false, @"自动随机方向变换图片出现异常bug");
    }
}

#pragma mark - Getters
- (UIScrollView *)scrollView{
    if (_scrollView == nil) {
        _scrollView = [UIScrollView new];
        _scrollView.backgroundColor = BackgroundColor240;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.bounces = NO;
        [self addSubview:_scrollView];
    }
    
    return _scrollView;
}

#ifndef kImageGalleryScrollViewTime
#define kImageGalleryScrollViewTime 3.0f
#endif

- (NSTimer *)timer{
    if (_timer == nil) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:kImageGalleryScrollViewTime
                                                  target:self
                                                selector:@selector(timerAction)
                                                userInfo:nil
                                                 repeats:YES];
    }
    
    return _timer;
}

#pragma mark - Setters
- (void)setModel:(nonnull NSArray *)model{
    _model = model;
    [self updateUI];
}

@end
